package com.si.util;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class FileUpload {
	static List<String> ilist =null;
	/**
	 * 图片和视频上传
	 * @param imgpath 上传路径
	 * @param imageName 上传的文件名
	 * @param request 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String picvideoupload(String imgpath,String imageName,HttpServletRequest request){
		String imgFileName = "";
		MultipartHttpServletRequest filerequest = (MultipartHttpServletRequest)request;
		String uploadImgPath = request.getRealPath(imgpath);
		System.out.println(uploadImgPath);
		File imgfile = new File(uploadImgPath);
		if(!imgfile.isFile()){
			imgfile.mkdirs();
		}
		MultipartFile upimgfile = (MultipartFile)filerequest.getFile(imageName);
        if(upimgfile!=null && !upimgfile.isEmpty()) {
        	imgFileName = upimgfile.getOriginalFilename();
        	
        	Random rd = new Random();
        	
        	imgFileName = DateUtil.format(new Date(), "yyyyMMddHHmmss")+rd.nextInt(10000) + imgFileName.substring(imgFileName.lastIndexOf("."),imgFileName.length());
        	File uploadFile = new File(uploadImgPath +"/"+ imgFileName);
        	try {
				OutputStream os = new FileOutputStream(uploadFile);
				InputStream is = upimgfile.getInputStream();
				byte[] bytes = new byte[1024];
				int c;
				while ((c = is.read(bytes)) != -1) {
					os.write(bytes, 0, c);
				}
				System.out.println("img upload success");
				is.close();
				os.close();
			} catch (Exception ex) {
				System.out.println("img not upload");
				System.out.println(ex.getMessage());
			}
        }
        return imgFileName;
	}
	
	/**
	 * 多个图片批量上传
	 * boolean b = FileUpload.picvideouploads("upload/kaika","img", request);
		for(String name : FileUpload.getIlist()){
			KaiKa kaiKa =new KaiKa();
			kaiKa.setImg("upload/kaika/" + name);
		}
	 * @param imgpath
	 * @param imageName
	 * @param request
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static boolean picvideouploads(String imgpath,String imageName,HttpServletRequest request){
		ilist =new LinkedList<String>();
		String imgFileName = "";
		MultipartHttpServletRequest filerequest = (MultipartHttpServletRequest)request;
		String uploadImgPath = request.getRealPath(imgpath);
		File imgfile = new File(uploadImgPath);
		if(!imgfile.isFile()){
			imgfile.mkdirs();
		}
		
		List<MultipartFile> list =filerequest.getFiles(imageName);
		for(MultipartFile upimgfile:list){
	        if(upimgfile!=null && !upimgfile.isEmpty()) {
	        	imgFileName = upimgfile.getOriginalFilename();
	        	
	        	Random rd = new Random();
	        	
	        	imgFileName = DateUtil.format(new Date(), "yyyyMMddHHmmss")+rd.nextInt(10000) + imgFileName.substring(imgFileName.lastIndexOf("."),imgFileName.length());
	        	File uploadFile = new File(uploadImgPath +"/"+ imgFileName);
	        	try {
					OutputStream os = new FileOutputStream(uploadFile);
					InputStream is = upimgfile.getInputStream();
					byte[] bytes = new byte[1024];
					int c;
					while ((c = is.read(bytes)) != -1) {
						os.write(bytes, 0, c);
					}
					System.out.println("img upload success");
					is.close();
					os.close();
				} catch (Exception ex) {
					System.out.println("img not upload");
					System.out.println(ex.getMessage());
				}
	        	ilist.add(imgFileName);
				System.out.println(imgFileName);
	        }
		}
        return true;
	}

	public static List<String> getIlist() {
		return ilist;
	}

	public static void setIlist(List<String> ilist) {
		FileUpload.ilist = ilist;
	}

	/**
	 * 文件大小转换
	 *
	 * @param
	 *
	 * @param
	 *
	 * @param
	 *            （B,K,M,G）
	 * @return
	 */
	public static String getPrintSize(long size) {
		//如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
		if (size < 1024) {
			return String.valueOf(size) + "B";
		} else {
			size = size / 1024;
		}
		//如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
		//因为还没有到达要使用另一个单位的时候
		//接下去以此类推
		if (size < 1024) {
			return String.valueOf(size) + "KB";
		} else {
			size = size / 1024;
		}
		if (size < 1024) {
			//因为如果以MB为单位的话，要保留最后1位小数，
			//因此，把此数乘以100之后再取余
			size = size * 100;
			return String.valueOf((size / 100)) + "."
					+ String.valueOf((size % 100)) + "MB";
		} else {
			//否则如果要以GB为单位的，先除于1024再作同样的处理
			size = size * 100 / 1024;
			return String.valueOf((size / 100)) + "."
					+ String.valueOf((size % 100)) + "GB";
		}
	}
}
